package com.punai.config.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.common.core.domain.CommonConstants;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.exception.ServiceException;
import com.punai.config.domain.ConfigFormatCategory;
import com.punai.config.domain.ConfigLinkTemplateFormacte;
import com.punai.config.mapper.ConfigFormatCategoryMapper;
import com.punai.config.mapper.ConfigGenerateCodeMapper;
import com.punai.config.mapper.ConfigLinkTemplateFormacteMapper;
import com.punai.config.service.ConfigFormatCategoryService;
import com.punai.config.sqlitedao.ConfigFormatCategoryRepository;
import com.punai.config.sqlitedao.ConfigTemplateLinkFormacategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ConfigFormatCategoryServiceImpl extends ServiceImpl<ConfigFormatCategoryMapper, ConfigFormatCategory> implements ConfigFormatCategoryService {

    @Autowired
    private ConfigFormatCategoryMapper configFormatCategoryMapper;
    @Autowired
    private ConfigLinkTemplateFormacteMapper configLinkTemplateFormacteMapper;

    @Autowired
    private ConfigFormatCategoryRepository configFormatCategoryRepository;
    @Autowired
    private ConfigTemplateLinkFormacategoryRepository configTemplateLinkFormacategoryRepository;

    @Override
    public List<TreeBo> treelist() {
        List<ConfigFormatCategory> configFormatCategories = configFormatCategoryMapper.selectList(null);
        return TreeBo.convert(configFormatCategories);
    }

    @Override
    @Transactional
    public int saveOrUpdateFormatCategory(ConfigFormatCategory configFormatCategory) throws ParseException {
        // 存在
        String id = configFormatCategory.getId();
        String code = configFormatCategory.getCode();
        if (StrUtil.isNotEmpty(id)) {
            configFormatCategoryMapper.update(configFormatCategory,Wrappers.<ConfigFormatCategory>lambdaUpdate().eq(ConfigFormatCategory::getId,id));
            configFormatCategoryRepository.update(configFormatCategory);
        } else {
            String pid = configFormatCategory.getPid();
            if (StrUtil.isBlank(pid)) {
                configFormatCategory.setPid(CommonConstants.DEF_PID);
            }
            Integer maxOrderNum = getMaxOrderNum(configFormatCategory.getPid());
            configFormatCategory.setOrderNum(maxOrderNum + 1);
            configFormatCategoryMapper.insert(configFormatCategory);
            configFormatCategoryRepository.save(configFormatCategory);
        }

        // 是必选的没?
        String templateIds = configFormatCategory.getTemplateIds();
        configLinkTemplateFormacteMapper.delete(
                Wrappers.<ConfigLinkTemplateFormacte>lambdaQuery()
                        .eq(ConfigLinkTemplateFormacte::getFormatcategoryCode,code)
        );
        configTemplateLinkFormacategoryRepository.deleteByFormacategoryCode(code);
        if (StrUtil.isNotEmpty(templateIds)) {
            List<String> templateIdList = StrUtil.split(templateIds, ",");
            List<ConfigLinkTemplateFormacte> links = new ArrayList<>();
            for (String templateId : templateIdList) {
                ConfigLinkTemplateFormacte link = new ConfigLinkTemplateFormacte();
                link.setTemplateId(templateId);
                link.setFormatcategoryCode(code);
                links.add(link);
            }
            configLinkTemplateFormacteMapper.insertBatchSomeColumn(links);
            configTemplateLinkFormacategoryRepository.save(links);
        }

        return 1;
    }

    private Integer getMaxOrderNum(String pid) {
        if (StrUtil.isBlank(pid)) {
            pid = CommonConstants.DEF_PID;
        }
        return configFormatCategoryMapper.selectMaxOrderNum(pid);
    }


    @Override
    public ConfigFormatCategory getInfo(String id) {
        ConfigFormatCategory configFormatCategory = configFormatCategoryMapper.selectById(id);
        String code = configFormatCategory.getCode();
        List<String> templateIdArr = configFormatCategory.getTemplateIdArr();
        List<ConfigLinkTemplateFormacte> configLinkTemplateFormactes = configLinkTemplateFormacteMapper.selectList(
                Wrappers.<ConfigLinkTemplateFormacte>lambdaQuery()
                        .eq(ConfigLinkTemplateFormacte::getFormatcategoryCode, code)
        );

        if (CollUtil.isNotEmpty(configLinkTemplateFormactes)) {
            for (ConfigLinkTemplateFormacte configLinkTemplateFormacte : configLinkTemplateFormactes) {
                templateIdArr.add(configLinkTemplateFormacte.getTemplateId());
            }
        }

        String classIds = configFormatCategory.getClassIds();
        if (StrUtil.isNotBlank(classIds)) {
            List<String> split = StrUtil.split(classIds, ",");
            configFormatCategory.setClassIdArr(split);
        }

        return configFormatCategory;
    }

    @Override
    @Transactional
    public int delete(String id) {
        if (isChildrenCheck(id)) {
            throw new ServiceException("存在子节点无法删除!");
        }
        ConfigFormatCategory configFormatCategory = configFormatCategoryMapper.selectById(id);
        String code = configFormatCategory.getCode();
        // 删除  模板关联
        configLinkTemplateFormacteMapper.delete(
                Wrappers.<ConfigLinkTemplateFormacte>lambdaQuery()
                        .eq(ConfigLinkTemplateFormacte::getFormatcategoryCode, code)
        );
        configTemplateLinkFormacategoryRepository.deleteByFormacategoryCode(code);
        configFormatCategoryRepository.delete(configFormatCategory);
        return configFormatCategoryMapper.deleteById(id);
    }



    private boolean isChildrenCheck(String id) {
        return configFormatCategoryMapper.selectCount(
                Wrappers.<ConfigFormatCategory>lambdaQuery()
                        .eq(ConfigFormatCategory::getPid,id)
        ) > 0;
    }

}
